package com.sk.aop.proxy;

import ch.qos.logback.classic.spi.EventArgUtil;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class JDKInvocationHandler implements InvocationHandler {
    private Object target;//目标对象

    public JDKInvocationHandler(Object target) {
        this.target = target;
    }

    /**
     * 代理对象 通过invoke调用 目标对象的方法
     * @param proxy the proxy instance that the method was invoked on
     *
     * @param method the {@code Method} instance corresponding to
     * the interface method invoked on the proxy instance.  The declaring
     * class of the {@code Method} object will be the interface that
     * the method was declared in, which may be a superinterface of the
     * proxy interface that the proxy class inherits the method through.
     *
     * @param args an array of objects containing the values of the
     * arguments passed in the method invocation on the proxy instance,
     * or {@code null} if interface method takes no arguments.
     * Arguments of primitive types are wrapped in instances of the
     * appropriate primitive wrapper class, such as
     * {@code java.lang.Integer} or {@code java.lang.Boolean}.
     *
     * @return
     * @throws Throwable
     */
    //JDK只能代理接口，不能代理类
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("我是代理，开始代理");
        //通过反射执行目标方法
        Object result = method.invoke(target, args);
        //出租后
        System.out.println("我是代理，结束代理");
        return result;
    }
}
